package net.trade.trade.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.core.base.struts.BaseAction;
import net.core.common.Constants;
import net.sf.json.JSONArray;
import net.system.sys.service.ICommService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("request")
@Namespace("/trade")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results({ @Result(name = "error", location = "/error.jsp") })
public class CommAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5261544250101287724L;

	final Logger logger = LoggerFactory.getLogger(CommAction.class);

	@Autowired
	private ICommService service;
	// 包含表格的一些信息，需要不变的传回去
	private String sEcho;

	// 当你点击下一页或者页数的时候会传到后台的值 （分页起始记录数）
	private int iDisplayStart;
	// 默认是传10 每页显示记录数
	private int iDisplayLength;
	// 当前页码
	private int pn = 1;
	
	private String pid;

	/**
	 * 列表查询action（列表内容展示action）异步
	 * 
	 * @Title: searchListData
	 * @return void
	 * @description
	 * @version 1.0
	 * @author dum
	 * @update 2012-10-17 下午04:42:17
	 */
	@Action(value = "searchListData")
	public void searchListData() {
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		try {
			iDisplayLength = 100;
			if (iDisplayStart != -1) {
				pn = iDisplayStart / iDisplayLength + 1;
			} else {
				String spn = request.getParameter("pn");
				pn = Integer.parseInt(spn != null ? spn : "1");
			}

			JSONArray jsonStr = null;
			try {
				jsonStr = JSONArray.fromObject(service.getTableInfo(pn,
						iDisplayLength, pid, request, sEcho));

			} catch (Exception e) {
				e.printStackTrace();
			}
			response.setContentType(Constants.SYS_CONTECTTYPE_JSON);
			response.getWriter().println(jsonStr);
		} catch (NullPointerException e) {
			logger.error("searchListData出错!NullPointe异常:", e);
		} catch (IOException e) {
			try {
				response.getWriter().println("查询出错！请联系管理员!");
			} catch (IOException e1) {
			}
			logger.error("searchListData出错!!", e);
		} catch (Exception e) {
			logger.error("查询数据其它错误!!", e);
		}
	}

	public String getsEcho() {
		return sEcho;
	}

	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}

	public int getiDisplayStart() {
		return iDisplayStart;
	}

	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}

	public int getiDisplayLength() {
		return iDisplayLength;
	}

	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}

	public int getPn() {
		return pn;
	}

	public void setPn(int pn) {
		this.pn = pn;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}
	
	
}
